﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.sysDAL;
using dhDataAcess.Model;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.baseBLL;
using dhDataAcess.linqDAL;

namespace dhSystemBLL.basicData
{
   public  class queryBLL
    {
       private queryTableAdapter _queryAdapter = null;
       private queryTableAdapter queryAdapter
        {
            get
            {
                if (_queryAdapter == null) _queryAdapter = new queryTableAdapter();
                return _queryAdapter;
            }
        }

        /// <summary>
        /// 查询查询表表格
        /// </summary>
        /// <returns></returns>

       public sysDataSet.queryDataTable getqueryAll()
        {
            try
            {
                return queryAdapter.GetDataqueryAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据查询编号查询查询表表格
       /// </summary>
       /// <returns></returns>

       public sysDataSet.queryDataTable getquerybyqueryNumAll(string  queryNum)
       {
           try
           {
               return queryAdapter.GetDataqueryByqueryNum(queryNum);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        ///整体更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int queryAllUpdateIII(sysDataSet.queryDataTable queryTable, queryMasterModel masterRow,string queryNum,string user,string modNum)
       {
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction(); //开始一个事务
               queryDAL query = new queryDAL(tn, conn);
               queryMasterDAL queryMaster = new queryMasterDAL(tn, conn);
               //string queryNum = "";
               queryMasterBLL queryMasterbll = new queryMasterBLL();
                sysDataSet.queryMasterDataTable q = queryMasterbll.getqueryMasterAll( user ,queryNum ,modNum );
                //string queryNum = null;

                    if (masterRow != null&&q.Rows.Count == 0)
                    {
                        queryMaster.queryMasterInsert(masterRow);
                        queryNum = masterRow.queryNum;
                    }
               
          
               foreach (sysDataSet.queryRow tRow in queryTable)
               {
                   queryModel queryRow = new queryModel();
                   if (tRow.RowState != DataRowState.Deleted)
                   {
                      
                       queryRow.strField = tRow.strField;
                       queryRow.strFieldParm = tRow.strFieldParm;
                       queryRow.strBJ = tRow.strBJ;
                       queryRow.strValue = tRow.strValue;
                       queryRow.strFieldDataType = tRow.strFieldDataType;
                       queryRow.strLog = tRow.strLog;
                       queryRow.updateDate = DateTime.Now;
                       queryRow.updateUser = "";
                       queryRow.createUser = tRow.createUser;
                       queryRow.createDate = tRow.createDate;
                       queryRow.mark = tRow.mark;
                       queryRow.userNum = tRow.userNum;
                       queryRow.queryNum = queryNum;
                       queryRow.querySerial = tRow.querySerial;
                       queryRow.strFieldDataType = tRow.strFieldDataType;
                       //query.queryInsert(queryRow);
                   }
                   if (tRow.RowState == DataRowState.Added)
                   {
                       query.queryInsert(queryRow);
                   }

                   if (tRow.RowState == DataRowState.Modified)
                   {
                       query.queryUpdate(queryRow);
                   }
                   if (tRow.RowState == DataRowState.Deleted)
                   {
                       DataRow row = (DataRow)tRow;
                       query.queryDel(row["recordID", DataRowVersion.Original].ToString());
                   }
               }
               tn.Commit(); //整体提交
               return i;
           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());
           }
           finally
           {
               conn.Close();
           }
       }
    

       public int queryAllUpdate(sysDataSet.queryDataTable queryTable,queryMasterModel masterRow,string tableName,string queryNum)
       {
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction(); //开始一个事务
               queryDAL query = new queryDAL(tn, conn);
               queryMasterDAL queryMaster = new queryMasterDAL(tn, conn);
               if (masterRow != null)
               {
                   queryMaster.queryMasterInsert(masterRow);
                   queryNum = masterRow.queryNum;
               }
               //itemListBLL item = new itemListBLL();
               //sysDataSet.itemListDataTable itemList = item.getitemListBymoduleNum(tableName);
               foreach (sysDataSet.queryRow tRow in queryTable)
               {
                   queryModel queryRow = new queryModel();
                   if (tRow.RowState != DataRowState.Deleted)
                   {
                      
                       //foreach (sysDataSet.itemListRow itemRow in itemList)
                       //{
                       //    if (tRow .strField  ==itemRow.itemDispC )
                       //    {
                       //        tRow.strField = itemRow.itemName;
                       //    }
                       //}
                       queryRow.strField = tRow.strField;
                       queryRow.strFieldParm = tRow.strFieldParm;
                       queryRow.strBJ = tRow.strBJ;
                       queryRow.strValue = tRow.strValue;
                       queryRow.strFieldDataType = tRow.strFieldDataType;
                       queryRow.strLog = tRow.strLog;
                       queryRow.updateDate = DateTime.Now;
                       queryRow.updateUser = "";
                       queryRow.createUser = tRow.createUser;
                       queryRow.createDate = tRow.createDate;
                       queryRow.mark = tRow.mark;
                       queryRow.userNum = tRow.userNum;
                       queryRow.queryNum = queryNum;
                       queryRow.querySerial = tRow.querySerial;
                       queryRow.strFieldDataType = tRow.strFieldDataType;
                      // queryRow.strFieldName = tRow.strFieldName;
                   }

                   if (tRow.RowState == DataRowState.Added)
                   {
                       query.queryInsert(queryRow);
                   }

                   if (tRow.RowState == DataRowState.Modified)
                   {
                       query.queryUpdate(queryRow);
                   }
                   if (tRow.RowState == DataRowState.Deleted)
                   {
                       DataRow row = (DataRow)tRow;
                       query.queryDel(row["recordID",DataRowVersion.Original].ToString());
                   }
                   
               }
               tn.Commit(); //整体提交
               return i;
           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());
           }
           finally
           {
               conn.Close();
           }
       }

       /// <summary>
       /// 插入信息
       /// </summary>
       /// <param name="row"></param>
       /// <returns></returns>
       public int queryInsert(queryModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                queryDAL query = new queryDAL(tn, conn);
                query.queryInsert(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

       public int queryUpdate(queryModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                queryDAL query = new queryDAL(tn, conn);
                query.queryUpdate(row);
                tn.Commit();
                return i;

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

       public int queryDel(string recordid)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                queryDAL query = new queryDAL(tn, conn);
                query.queryDel(recordid);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
   
    }
}
